<?php
/*
 * @Author: zwx
 * @Date: 2024-05-29 01:58:22
 * @LastEditors: zwx
 * @LastEditTime: 2024-06-09 16:06:05
 * @Description:
 */

namespace app\apiadmin\service\data;

use Predis\Client;

class RedisService
{

    /**
     * make redis service
     *
     * @return Client client
     */
    public static function makeService()
    {
        return new Client([
            'scheme' => 'tcp',
            'host' => env('redis.host'),
            'port' => env('redis.port'),
            "password" => env('redis.password'),
            "username" => env('redis.username'),

        ]);
    }

    /**
     * 构造一个 读写不超时的客户端
     * @return Client
     */
    public static function makeServiceUnlimit()
    {

        $option = [
            'scheme' => 'tcp',
            'host' => env('redis.host'),
            'port' => env('redis.port'),
        ];
        if (env('redis.password') != "") {
            $option["password"] = env('redis.password');
        }

        if (env('redis.username') != '') {
            $option['username'] = env('redis.username');
        }
        print_r($option);
        return new Client($option + [
            'read_write_timeout' => 0,
        ]);
    }

    /**
     * @author: zwx
     * @Description: 获取开播时间
     * @param {*} $liveId
     * @Date: 2024-06-09 16:06:35
     */
    public static function getLiveStartTime($liveId)
    {

        $redis = self::makeService();
        // 如果 reids 中有那么 直接从reids中获取到数据
        $key = env("redis.prefix") . "live:info:" . $liveId;
        $info = $redis->get($key);
        if ($info) {
            $live = json_decode($info, true);
            return $live['service_start_time']; //消息发送时间（相对于开播时间）
        } else {
            return false;
        }

    }
}
